﻿/// <reference path="Controls/BaseComplexControl.js" />


var $TabContainer = TA.Turbo.Controls.TabContainer = function TabContainer(clientId)
{
	this.BaseComplexControl(clientId);

	this._panels = new Array();
}
TA.Turbo.Controls.TabContainer.Inherit($BaseComplexControl);

var _____a;
if (_____a)
{
	$TabContainer = $BaseComplexControl;
//	$TabContainer.prototype = new $BaseComplexControl();
	$TabContainer.prototype.BaseComplexControl = new $BaseComplexControl();
}

$PropertyDefinition.Define($TabContainer, "ActiveTabIndex", "AT", 0, {
OnChanging: function OnChanging(args)
	{
		this.RaiseEvent("ActiveTabIndexChanging", args);
	},
	OnChanged: function OnChanged()
	{
		this.RaiseEvent("ActiveTabIndexChanged");
		
		this.UpdateDisplay();
	}
});
$PropertyDefinition.Define($TabContainer, "Height", "H", null, {
	OnChanged: function OnChanged()
	{
		var p = this._panels, pl = p.length;
		for(var i = 0; i < pl; ++i)
		{
			p[i].style.height = this.get_Height() == null ? "" : this.get_Height() + "px";
		}
	}
});

$TabContainer.DefineEvent(new $ServerEvent("ActiveTabIndexChanging"));
$TabContainer.DefineEvent(new $ServerEvent("ActiveTabIndexChanged"));

$TabContainer.prototype._panels = new Array();
$TabContainer.prototype._divHeader = document.createElement("a");
$TabContainer.prototype._divMain = document.createElement("a");

$TabContainer.prototype.get_ActiveTab = function()
{
	return this._panels[this._properties.ActiveTabIndex];
}

$TabContainer.prototype.Init = function Init()
{
	this.BaseComplexControl.Init.call(this);

	var cn = this.get_Control().get_ChildNodes(), cnl = cn.length;
	for (var i = 0; i < cnl; ++i)
	{
		switch (cn[i].className)
		{
			case "header":
				this._divHeader = cn[i];
				break;
			case "body":
				this._divMain = cn[i];
				break;
		}
	}
}
$TabContainer.prototype.Load = function Load()
{
	this.BaseComplexControl.Load.call(this);
	var iPanel = -1;
	
	var mainChildNodes = this._divMain.get_ChildNodes(),
		headerChildNodes = this._divHeader.get_ChildNodes();
	for (var iHeader = 0; iHeader < headerChildNodes.length; ++iHeader)
	{
		do
		{
			++iPanel;
		}
		while (mainChildNodes[iPanel].tagName != "DIV");
		
		var pnl = mainChildNodes[iPanel].get_Object();
		this._panels.push(pnl);
		pnl._tabContainer = this;

		pnl._header = headerChildNodes[iHeader];
		pnl._header.AddEvent("click", this._tabHeader_Click, this, iHeader);
	}
}
$TabContainer.prototype.UpdateDisplay = function UpdateDisplay()
{
	if (this.get_ActiveTabIndex() < 0) this.set_ActiveTabIndex(0);
	if (this.get_ActiveTabIndex() > this._panels.length - 1) this.set_ActiveTabIndex(this._panels.length - 1);

	if (!this.get_ActiveTab().get_Enabled())
	{
		for (var i = 1; i <= Math.max(this.get_ActiveTabIndex(), this._panels.length - this.get_ActiveTabIndex()); ++i)
		{
			if (this.get_ActiveTabIndex() + i < this._panels.length)
			{
				if (this._panels[this._properties.ActiveTabIndex + i].get_Enabled())
				{
					this.set_ActiveTabIndex(this.get_ActiveTabIndex() + i);
					break;
				}
			}
			if (this.get_ActiveTabIndex() - i >= 0)
			{
				if (this._panels[this.get_ActiveTabIndex() - i].get_Enabled())
				{
					this.set_ActiveTabIndex(this.get_ActiveTabIndex() - i);
					break;
				}
			}
		}
	}

	for (var i = 0; i < this._panels.length; ++i)
	{
		var pnl = this._panels[i];
		pnl._header.className =
			pnl.get_Control().className =
			this.get_ActiveTabIndex() == i ? "active" : "";

		pnl.get_Control().style["display"] = this.get_ActiveTabIndex() == i ? "" : "none";
	}
}

$TabContainer.prototype._tabHeader_Click = function(index, e)
{
	this.set_ActiveTabIndex(index);
}






TA.Turbo.Controls.TabPanel = function TabPanel(clientId)
{
	this.BaseComplexControl(clientId);
}

TA.Turbo.Controls.TabPanel.Inherit($BaseComplexControl);
if (_____a)
{
	TA.Turbo.Controls.TabPanel.prototype = new $BaseComplexControl();
	TA.Turbo.Controls.TabPanel.prototype.BaseComplexControl = new $BaseComplexControl();
}
var $TabPanel = TA.Turbo.Controls.TabPanel;

$PropertyDefinition.Define($TabPanel, "Header", true, { Text: "Tab" }, null, null, function()
{
	this._header.innerHTML = this.get_Header().Text;
});

$TabPanel.prototype._tabContainer = null;
$TabPanel.prototype._header = null;

if (_____a) $TabPanel.prototype._tabContainer = new TabContainer();
if (_____a) $TabPanel.prototype._header = document.createElement("asd");

$TabPanel.prototype.Init = function Init()
{
	this.BaseComplexControl.Init.call(this);
}

$TabPanel.prototype._OnEnabledChanged = function _OnEnabledChanged()
{
	this._header.style.display = this.get_Enabled() ? "inline-block" : "none";

	this._tabContainer.UpdateDisplay();
}
